package com.test.qnews.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.test.qnews.R;
import com.test.qnews.adapter.CommonAdapter;
import com.test.qnews.beans.GsonBean;
import com.test.qnews.beans.GsonMicroblogBeans;
import com.test.qnews.beans.NetDataBean;
import com.test.qnews.volley.VolleySingleton;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/20.
 */
public class MicroblogFragment extends Fragment {
    private ListView listView;
    private CommonAdapter commonAdapter;
    private List<NetDataBean> datas;
    private Context context;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context = context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_recommend, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        listView = (ListView) view.findViewById(R.id.lv_recommend);

        data();
    }

    private void data() {

            Intent intent = getActivity().getIntent();
            String url = intent.getStringExtra("urlMi");

            VolleySingleton.getInstance().addRequest(url, new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                    Gson gson = new Gson();
                    datas = new ArrayList<>();
                    GsonMicroblogBeans gsonMicroblogBeans = gson.fromJson(response, GsonMicroblogBeans.class);

                    for (int i = 0; i < gsonMicroblogBeans.getData().getDetail().getContent().getWeibo_list().size(); i++) {
                        NetDataBean netDataBean = new NetDataBean();
                        netDataBean.setTitle(gsonMicroblogBeans.getData().getDetail().getContent().
                                getWeibo_list().get(i).getSummary());
//                    netDataBean.setTime(gsonBean.getData().getArticle().get(i).getComment_cnt());
                        netDataBean.setItem_type(gsonMicroblogBeans.getData().getDetail().getType()
                        );
                        Log.d("MicroblogFragment", "gsonMicroblogBeans.getData().getDetail().getContent().getWeibo_list().get(i).getThumbnails().size():" + gsonMicroblogBeans.getData().getDetail().getContent().getWeibo_list().get(i).getThumbnails().size());
                        for (int j = 0; j < gsonMicroblogBeans.getData().getDetail().getContent().getWeibo_list().
                                get(i).getThumbnails().size(); j++) {
                            if (gsonMicroblogBeans.getData().getDetail().getContent().getWeibo_list().get(i).getThumbnails().size() > 0) {
                                netDataBean.setImageUrl(gsonMicroblogBeans.getData().getDetail().getContent().getWeibo_list().
                                        get(i).getThumbnails().get(0).getUrl());
                            }
                        }
                        datas.add(netDataBean);

                    }
//                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//                    @Override
//                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                        Intent intent = new Intent(getActivity(), DetailsActivity.class);
//                        intent.putExtra("pos",position);
//                        intent.putExtra("data", (Serializable) datas);
//                        startActivity(intent);
//                    }
//                });


                    commonAdapter = new CommonAdapter(datas, context);
                    listView.setAdapter(commonAdapter);

                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });
        }
    }


